package gwtappcontainer.server.apps.geolocation;

import gwtappcontainer.server.apps.content.ContentAPI;
import gwtappcontainer.shared.apis.APIResponse;
import gwtappcontainer.shared.apis.APIResponse.Status;
import gwtappcontainer.shared.apps.geolocation.LocationProp;

import java.util.Map;

import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;

@Api(name = "geolocation", 
scopes = { "https://www.googleapis.com/auth/userinfo.email" }
)
public class GeoLocationAPI {
	@ApiMethod(httpMethod = HttpMethod.GET, path = "getlocation")
	public APIResponse getLocation(HttpServletRequest req) {
		try {
			LocationProp prop = new LocationProp();
			
			prop.country = req.getHeader("X-AppEngine-Country");
			prop.region = req.getHeader("X-AppEngine-Region");
			prop.city = req.getHeader("X-AppEngine-City");
			prop.latLong = req.getHeader("X-AppEngine-CityLatLong");
			
			APIResponse response = new APIResponse(Status.SUCCESS, prop);
			
			return response;	
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
	
	@ApiMethod(httpMethod = HttpMethod.GET, path = "getcontentforcity")
	public APIResponse getContentForCity(@Named("tag") String tag, 
			@Named("city") String city) {
		try {
			if (null == city) city = "";
			tag = tag.toLowerCase();
			city = city.toLowerCase();
			
			ContentAPI api = new ContentAPI();
			
			@SuppressWarnings("unchecked")
			Map<String, String> all = 
				(Map<String, String>) api.getAllValidPublishedContents().object;
			
			//check if tag + "_" + city is present
			String tagForCity = tag + "_" + city;
			if (all.containsKey(tagForCity)) {
				return new APIResponse(Status.SUCCESS, all.get(tagForCity));
			}
			
			//or tag + "_" + default is present
			String defaultTag = tag + "_default";
			if (all.containsKey(defaultTag)) {
				return new APIResponse(Status.SUCCESS, all.get(defaultTag));
			}
			
			//return resource not found error
			APIResponse response = new APIResponse();
			response.statusCode = Status.ERROR_RESOURCE_DOES_NOT_EXIST;
			response.userFriendlyMessage = "Could find neither tag for city [" +
					tagForCity + "] nor default tag [" + defaultTag + "]. " + 
					"It could be that tags have empty content or content is not published.";
			
			return response;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
		
	}
	
	@ApiMethod(httpMethod = HttpMethod.GET, path = "getmostrelevantcontent")
	public APIResponse getMostRelevantContent(@Named("tag") String tag, 
			@Named("city") String city, 
			@Named("region") String region, 
			@Named("country") String country) {
			
		try {
			if (null == city) city = "";
			if (null == region) region = "";
			if (null == country) country = "";
			
			tag = tag.toLowerCase();
			city = city.toLowerCase();
			region = region.toLowerCase();
			country = country.toLowerCase();
			
			ContentAPI api = new ContentAPI();
			
			@SuppressWarnings("unchecked")
			Map<String, String> all = 
				(Map<String, String>) api.getAllValidPublishedContents().object;
			
			//check if tag + "_" + city is present
			String tagForCity = tag + "_" + city;
			if (all.containsKey(tagForCity)) {
				return new APIResponse(Status.SUCCESS, 
						all.get(tagForCity), "content tag = [" + tagForCity + "]");
			}
			
			//check if tag + "_" + region is present
			String tagForRegion = tag + "_" + region;
			if (all.containsKey(tagForRegion)) {
				return new APIResponse(Status.SUCCESS, 
						all.get(tagForRegion), "content tag = [" + tagForRegion + "]");
			}
			
			//check if tag + "_" + country is present
			String tagForCountry = tag + "_" + country;
			if (all.containsKey(tagForCountry)) {
				return new APIResponse(Status.SUCCESS, 
						all.get(tagForCountry), "content tag = [" + tagForCountry + "]");
			}
					
			//or tag + "_" + default is present
			String defaultTag = tag + "_default";
			if (all.containsKey(defaultTag)) {
				return new APIResponse(Status.SUCCESS, all.get(defaultTag), 
						"content tag = [" + defaultTag + "]");
			}
			
			//return resource not found error
			APIResponse response = new APIResponse();
			response.statusCode = Status.ERROR_RESOURCE_DOES_NOT_EXIST;
			response.userFriendlyMessage = "Could find neither tag for city [" +
					tagForCity + "] nor tag for region [" + tagForRegion + 
					"] nor tag for country [" + tagForCountry + 
					"] nor default tag [" + defaultTag + "]. <br>" +
					"It could be that tags have empty content or content is not published.";
			
			return response;
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}		
}
